# TCU应用Makefile
# 作者: 开发者
# 日期: 2024

# 项目信息
PROJECT_NAME = tcu
VERSION = 1.0.0

# 目录定义
SRC_DIR = src
INCLUDE_DIR = include
BUILD_DIR = ../../build/tcu
CONFIG_DIR = config
OUTPUT_DIR = ../../out/tcu

# 编译器设置
CC = gcc
CFLAGS = -Wall -Wextra -O2 -std=c99 -g -D_GNU_SOURCE
INCLUDES = -I$(INCLUDE_DIR) -I../../log -I../../third_party/cJSON
LDFLAGS = -lpthread -lm

# 依赖库
CJSON_SOURCE = ../../third_party/cJSON/cJSON.c
PAHO_MQTT_LIB = -lpaho-mqtt3c
OPENSSL_LIB = -lssl -lcrypto

# 源文件
SOURCES = $(wildcard $(SRC_DIR)/*.c) ../../log/logger.c $(CJSON_SOURCE)
OBJECTS = $(SOURCES:%.c=$(BUILD_DIR)/%.o)
HEADERS = $(wildcard $(INCLUDE_DIR)/*.h)

# 目标文件
TARGET = $(OUTPUT_DIR)/$(PROJECT_NAME)

# 默认目标
all: $(TARGET)

# 创建目标文件
$(TARGET): $(OBJECTS) | $(BUILD_DIR) $(OUTPUT_DIR)
	@echo "链接 $(PROJECT_NAME)..."
	$(CC) $(OBJECTS) -o $@ $(LDFLAGS) $(OPENSSL_LIB)
	@echo "构建完成: $@"

# 编译源文件
$(BUILD_DIR)/%.o: %.c $(HEADERS) | $(BUILD_DIR)
	@echo "编译 $<..."
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

# 创建构建目录
$(BUILD_DIR):
	@mkdir -p $(BUILD_DIR)
	@mkdir -p $(BUILD_DIR)/$(SRC_DIR)

# 创建输出目录
$(OUTPUT_DIR):
	@mkdir -p $(OUTPUT_DIR)

# 清理
clean:
	@echo "清理构建文件..."
	rm -rf $(BUILD_DIR)
	@echo "清理完成"

# 安装
install: $(TARGET)
	@echo "安装 $(PROJECT_NAME)..."
	@mkdir -p /usr/local/bin
	@mkdir -p /usr/local/share/$(PROJECT_NAME)/config
	@mkdir -p /usr/local/include/$(PROJECT_NAME)
	cp $(TARGET) /usr/local/bin/
	cp -r $(CONFIG_DIR)/* /usr/local/share/$(PROJECT_NAME)/config/
	cp $(INCLUDE_DIR)/*.h /usr/local/include/$(PROJECT_NAME)/
	@echo "安装完成"

# 卸载
uninstall:
	@echo "卸载 $(PROJECT_NAME)..."
	rm -f /usr/local/bin/$(PROJECT_NAME)
	rm -rf /usr/local/share/$(PROJECT_NAME)
	rm -rf /usr/local/include/$(PROJECT_NAME)
	@echo "卸载完成"

# 运行
run: $(TARGET)
	@echo "运行 $(PROJECT_NAME)..."
	cd $(BUILD_DIR) && ./$(PROJECT_NAME) -c ../$(CONFIG_DIR)

# 调试运行
debug: $(TARGET)
	@echo "调试运行 $(PROJECT_NAME)..."
	cd $(BUILD_DIR) && gdb ./$(PROJECT_NAME) --args -c ../$(CONFIG_DIR)

# 检查依赖
check-deps:
	@echo "检查依赖库..."
	@pkg-config --exists libcjson && echo "✓ cJSON库已安装" || echo "✗ cJSON库未安装"
	@pkg-config --exists openssl && echo "✓ OpenSSL库已安装" || echo "✗ OpenSSL库未安装"
	@ldconfig -p | grep -q libpaho-mqtt3c && echo "✓ Paho MQTT C库已安装" || echo "✗ Paho MQTT C库未安装"

# 安装依赖 (Ubuntu/Debian)
install-deps:
	@echo "安装依赖库..."
	sudo apt-get update
	sudo apt-get install -y libcjson-dev libssl-dev libpaho-mqtt-dev

# 测试
test: $(TARGET)
	@echo "运行测试..."
	cd $(BUILD_DIR) && ./$(PROJECT_NAME) --help

# 打包
package: clean $(TARGET)
	@echo "打包 $(PROJECT_NAME)..."
	@mkdir -p $(BUILD_DIR)/package/$(PROJECT_NAME)-$(VERSION)
	cp -r $(CONFIG_DIR) $(BUILD_DIR)/package/$(PROJECT_NAME)-$(VERSION)/
	cp $(TARGET) $(BUILD_DIR)/package/$(PROJECT_NAME)-$(VERSION)/
	cp README.md $(BUILD_DIR)/package/$(PROJECT_NAME)-$(VERSION)/ 2>/dev/null || true
	cd $(BUILD_DIR)/package && tar -czf $(PROJECT_NAME)-$(VERSION).tar.gz $(PROJECT_NAME)-$(VERSION)
	@echo "打包完成: $(BUILD_DIR)/package/$(PROJECT_NAME)-$(VERSION).tar.gz"

# 显示帮助
help:
	@echo "TCU应用构建系统"
	@echo ""
	@echo "可用目标:"
	@echo "  all          - 构建项目 (默认)"
	@echo "  clean        - 清理构建文件"
	@echo "  install      - 安装到系统"
	@echo "  uninstall    - 从系统卸载"
	@echo "  run          - 运行应用"
	@echo "  debug        - 调试运行"
	@echo "  check-deps   - 检查依赖库"
	@echo "  install-deps - 安装依赖库 (Ubuntu/Debian)"
	@echo "  test         - 运行测试"
	@echo "  package      - 打包发布"
	@echo "  help         - 显示此帮助"
	@echo ""
	@echo "示例:"
	@echo "  make all"
	@echo "  make run"
	@echo "  make clean"

# 声明伪目标
.PHONY: all clean install uninstall run debug check-deps install-deps test package help
